<?php

function getPost($isVarName) {
	if(isset($_POST[$isVarName])) {
		return htmlspecialchars($_POST[$isVarName]);
	} else {
		return null;
	}
}

function getGet($isVarName) {
	if(isset($_GET[$isVarName])) {
		return htmlspecialchars($_GET[$isVarName]);
	} else {
		return null;
	}
}

function getTime() {
	return Date("Y-m-d H:i:s");
}

function getMaxTime() {
	return Date('Y-m-d H:i:s', PHP_INT_MAX);
}

function addError($isError, $isVarName, $isText) {
	$isError = $isError.$isVarName." - ".$isText."<br />";
	return $isError;
}

function mandatory($isVarName, $isInputName, &$isError) {
	if(empty($isVarName)) {
		$isError = addError($isError, $isInputName, 'povinný údaj');
	}
	return $isVarName;
}

function nvl($var1, $var2) {
	if(empty($var1)) {
		return $var2;
	} else {
		return $var1;
	}
}

function getSession($isVarName) {
	if(isset($_SESSION[$isVarName])) {
		return $_SESSION[$isVarName];
	} else {
		return null;
	}
}

function setSession($isVarName, $isVarValue) {
	$_SESSION[$isVarName] = $isVarValue;
}

function deleteSession($isVarName) {
	unset($_SESSION[$isVarName]); 
}

function init() {
	//if(!isset($_SESSION['year'])) { $_SESSION['year']=DATE("Y"); }
}

function redirect($isURL) {
	header("Location: ".$isURL);
}

function getFullURL() {
	$lsPageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") {$lsPageURL .= "s";}
	$lsPageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
	 $lsPageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	} else {
	 $lsPageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}
	return $lsPageURL;
}

function getURL() {
	return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

function isLogged() {
	$lrLogin = getSession("Login");
	if(isset($lrLogin)) {
		return TRUE;
	} else {
		return FALSE;
	}
}

function loginRequired($inLevel) {
	if(isLogged()) {
		$lrLogin = getSession("Login");
		if($inLevel > $lrLogin["level"]) {
			echo "Nedostatecna opravneni!";
			echo "LoginLevel: ".$lrLogin["level"];
		}
		
	} else {
		redirect(getURL()."?module=Auth&action=login");
	}
	//$lsLogin = getSession("Lo");
}

function generatePassword($isPassword) {
	global $lsPasswordSeed;
	return SHA1($lsPasswordSeed.$isPassword.$lsPasswordSeed);
}

/** byl odeslan formular? 
 * @ $isFormAction - name stisknuteho tlacitka
 **/
function IsFormSent($isFormAction) {
	$lsVar = getPost($isFormAction);
	if(!empty($lsVar)) {
		return TRUE;
	} else {
		return FALSE;
	}
}

function indent($lnSpaceCount) {
	$lsReturn = "";
	for ($i=0; $i <= $lnSpaceCount; $i++) { 
		$lsReturn .= "&nbsp;&nbsp;&nbsp;";
	}
	return $lsReturn;
}

/*************************************************************************************************************************************/

class systemBase {
	private $bgModulesData = array();
	private $lsError;

	public $csModuleName;
	public $Model;
	public $Smarty;
	public $csTemplateAction;
	public $Debug;

	public function __construct($lsModuleName) {
		global $background_active, $background_modules, $dbg_enable;
		$this->csModuleName = $lsModuleName;

		/* nacteni modelu pro controller */
		require "./app/model/M".$this->csModuleName.".php";
		$lsClassName = "M".$this->csModuleName."Class";
		$this->Model = new $lsClassName;

		/* nacteni smarty */
		$this->Smarty = new Smarty;
		//$smarty->force_compile = true;
		$this->Smarty->debugging = true;
		$this->Smarty->caching = false;
		$this->Smarty->cache_lifetime = 0;
		$this->Smarty->setTemplateDir('./app/application');

		/* nacteni debugu */
		$this->Debug = new systemDebug($dbg_enable, "debug.txt");

		/* zpracovani akci */
		$lsAction = getGet("action");
		if(empty($lsAction)) {
			$lsAction = "default"; // defaultni akce modulu
		}
		$lsFunctionName = "action".$lsAction;
		$this->$lsFunctionName();

		/* moduly na pozadi */
		if($background_active) {
			foreach ($background_modules as $lsModuleName) {
				$bgModulesData[$lsModuleName] = $this->RunBackgroundModule($lsModuleName);
			}
			$this->Smarty->assign("BackgroundModulesData", $bgModulesData);
		}

		/* Nastaveni erroru do template */
		$this->Smarty->assign("error", $this->lsError);

		/* zobrazeni template */
		$this->setBaseVariables();
		if(empty($this->csTemplateAction)) {
			$this->csTemplateAction = $lsAction;
		}
		$this->Smarty->assign("content", "T".$this->csModuleName."_".$this->csTemplateAction.".tpl");
		$this->Smarty->display("index.tpl");
	}

	private function setBaseVariables() {
		global $path_root, $path_img, $path_js;
		$this->Smarty->assign("PATH_ROOT", $path_root);
		$this->Smarty->assign("PATH_IMG", $path_img);
	}

	private function RunBackgroundModule($isModuleName) {
		try{
			$lsModuleName = "B".$isModuleName;
			require "./app/controller/background/".$lsModuleName.".php";
			$lrBGModule = new $lsModuleName($isModuleName);
			return $lrBGModule->Run();
		}
		catch (Exception $e) {
			$this->addError("Exception: ".$e->getMessage());
		}
	}

	private function loadClass($isClassName) {
		try{
			require "./app/controller/".$isClassName.".php";
			$lrModules[$isClassName] = new $isClassName($isClassName);
		}
		catch (Exception $e) {
			$this->addError("Exception: ".$e->getMessage());
		}

	}

	public function addError($isText) {
		$this->lsError = $this->lsError.$isText."<br />";
	}

	public function getError() {
		return $this->lsError;
	}

	// pokued nastala nejaka chyba
	public function IfError() {
		if(empty($this->lsError)) {
			return FALSE;
		} else {
			return TRUE;
		}
	}

	public function setError($isError) {
		$this->lsError = $isError;
	}

	public function actionDefault() {

	}

	public function viewDetail() {

	}

}

class backgroundBase {
	public $Model;
	public $Debug;

	public function __construct($isModuleName) {
		global $dbg_enable;

		/* nacteni modelu pro controller */
		require "./app/model/M".$isModuleName.".php";
		$lsClassName = "M".$isModuleName."Class";
		$this->Model = new $lsClassName;

		/* nacteni debugu */
		$this->Debug = new systemDebug($dbg_enable, "BackgroundDebug.txt");
	}
}

class systemDebug {
	private $lbDebug;
	private $lsFileName;

	private function write($isText) {
		$ldDate = new DateTime();
		$lsDate = $ldDate->format('d.m.Y H:i:s');

		$lfFile = fopen("./logs/".$this->lsFileName, "a");
		fwrite($lfFile, $lsDate.": ".$isText."\r\n");
		fclose($lfFile);
	}

	public function __construct($ibDbgEnable, $isFileName) {
		$this->lbDebug = $ibDbgEnable;
		$this->lsFileName = $isFileName;
	}

	public function pl($text) {
		if($this->lbDebug) {
			$this->write($text);
		}
	}

	public function vl($var, $value) {
		if($this->lbDebug) {
			$this->write($var." = ".$value);
		}
	}
}